package com.spdzen.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.service.SysUserRoleService;
import com.spdzen.support.LogUtils;
import com.spdzen.support.SimpleStringUtils;
import com.spdzen.support.result.JsonResult;

@Controller
@RequestMapping("/admin/sysuser")
public class SysUserRoleAdminController {
	@Resource
	private SysUserRoleService sysUserRoleService;

	@RequestMapping("/queryUserRoles.do")
	@ResponseBody
	public JsonResult queryUserRoles(Long sysUserId) {
		if (sysUserId == null || sysUserId.longValue() < 1) {
			return JsonResult.errorParameter();
		}

		try {
			return JsonResult.success(sysUserRoleService.selectByUser(sysUserId));
		} catch (Exception e) {
			LogUtils.getLogger().error(sysUserId.toString(), e);
			return JsonResult.error();
		}
	}

	@RequestMapping("/bindRoles.do")
	@ResponseBody
	public JsonResult bindRoles(Long sysUserId, String sysRoleIds) {
		if (sysUserId == null || sysUserId.longValue() < 1) {
			return JsonResult.errorParameter();
		}

		List<Long> tmpids = SimpleStringUtils.toLongList(sysRoleIds);
		List<Long> rids = new ArrayList<Long>();
		for (Long id : tmpids) {
			if (id.longValue() > 0) {
				rids.add(id);
			}
		}

		try {
			sysUserRoleService.bindRoles(sysUserId, rids);
			return JsonResult.success();
		} catch (Exception e) {
			LogUtils.getLogger().error(sysUserId.toString(), e);
			return JsonResult.error();
		}
	}
}
